﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace Warehouse.Web.UserControls
{
    public partial class PageNavControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            BindData();
        }

        private int? _PageIndex = null;
        public int PageIndex
        {
            get
            {
                if (!_PageIndex.HasValue)
                {
                    if (!string.IsNullOrEmpty(Request.QueryString["Page"]))
                    {
                        int p;
                        if (int.TryParse(Request.QueryString["Page"], out p))
                        {
                            _PageIndex = p - 1;
                        }
                    }
                    if (!_PageIndex.HasValue)
                    {
                        _PageIndex = 0;
                    }
                    if (_PageIndex < 0)
                    {
                        _PageIndex = 0;
                    }
                }
                return _PageIndex.Value;
            }
            set
            {
                _PageIndex = value < 0 ? 0 : value;
            }
        }

        private int _TotalPage = 1;
        public int TotalPage
        {
            get { return _TotalPage; }
            set
            {
                if (value < 1)
                {
                    _TotalPage = 1;
                }
                else
                {
                    _TotalPage = value;
                }
            }
        }

        private int _ShowPageCount = 10;
        public int ShowPageCount
        {
            get { return _ShowPageCount; }
            set { _ShowPageCount = value; }
        }

        public void SetTotalPage(int paseSize, long totalRows)
        {
            this.TotalPage = (int)(totalRows / paseSize + (totalRows % paseSize != 0 ? 1 : 0));
        }

        void BindData()
        {
            List<string> querys = new List<string>();
            foreach (var k in Request.QueryString.AllKeys)
            {
                if (k == "Page")
                {
                    continue;
                }
                querys.Add(k + "=" + Server.UrlEncode(Request.QueryString[k]));
            }
            querys.Add("Page=");
            string url = Request.Url.AbsolutePath + "?" + string.Join("&", querys.ToArray());
            this.tbUrl.Value = url;

            StringBuilder html = new StringBuilder();

            var totalcount = ShowPageCount;
            var end = PageIndex + totalcount / 2 + 2;
            if (end > TotalPage + 1)
            {
                end = TotalPage + 1;
            }
            var start = end - totalcount;
            if (start < 1)
            {
                start = 1;
                end = Math.Min(TotalPage + 1, start + totalcount);
            }

            for (var page = start; page < end; page++)
            {
                if (page != PageIndex + 1)
                {
                    html.Append(string.Format("<a href='{0}'>{1}</a>", url + page, page));
                }
                else
                {
                    html.Append(string.Format("<strong>{0}</strong>", page));
                }
            }
            this.ltPages.Text = html.ToString();

            this.lnkPrePage.NavigateUrl = url + (PageIndex);
            this.lnkNextPage.NavigateUrl = url + (PageIndex + 2);

            if (PageIndex <= 0)
            {
                this.lnkPrePage.Visible = false;
            }
            if (PageIndex >= TotalPage - 1)
            {
                this.lnkNextPage.Visible = false;
            }

            this.lbPageCount.Text = this.TotalPage.ToString();
        }
    }
}